package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

/**
 * 声明队列监听,回调函数
 */
@Component//声明为Spring中的Bean
public class SpringRabbitListener {
    /*@RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message) {
        System.err.println("消费者接收到simple.queue的消息:[" + message + "]");//运行启动类将会开始监听消息队列
        // console打印:消费者接收到simple.queue的消息:[Hello , Spring AMQP !]
    }*/

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.out.println("消费者1接收到消息:[" + message + "]" + LocalTime.now());
        Thread.sleep(20);
    }


    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.err.println("消费者2.......接收到消息:[" + message + "]" + LocalTime.now());
        Thread.sleep(200);
    }


    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) {
        System.out.println("消费者接收到fanout.queue1的消息:[" + message + "]");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) {
        System.err.println("消费者接收到fanout.queue2的消息:[" + message + "]");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}//{}表示数组
    ))//以注解的形式创建Exchange,Queue并绑定,以及声明BindingKey,同时声明队列监听,回调函数
    public void listenDirectQueue1(String message) {
        System.out.println("消费者接收到direct.queue1的消息:[" + message + "]");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}//{}表示数组
    ))//以注解的形式创建Exchange,Queue并绑定,以及声明bindingkey,同时声明队列监听,回调函数
    public void listenDirectQueue2(String message) {
        System.err.println("消费者接收到direct.queue2的消息:[" + message + "]");
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue1"),
                    exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
                    key = "china.#"
            )
    )
    public void listenTopicQueue1(String message) {
        System.out.println("消费者接收到topic.queue1的消息:[" + message + "]");
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(name = "topic.queue2"),
                    exchange = @Exchange(name = "itcast.topic", type = ExchangeTypes.TOPIC),
                    key = "#.news"
            )
    )
    public void listenTopicQueue2(String message) {
        System.out.println("消费者接收到topic.queue2的消息:[" + message + "]");
    }


    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object> msg) {//使用了自定义的MessageConverter转换器后发消息的时候是什么类型,监听回调函数形参就用什么类型接收消息
        System.err.println("接收到object.queue的消息:" + msg);//接收到object.queue的消息:{name=王天培, age=25}
    }
}
